/*
Eteria IRC Client, an RFC 1459 compliant client program written in Java.
Copyright (C) 2000-2001  Javier Kohen <jkohen at tough.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.*;
import ar.com.jkohen.awt.ImageButton;
import ar.com.jkohen.util.Resources;

public class ControlPanel extends Panel implements ActionListener
{
	public static final int CHANNELS = 0;
	public static final int WHO = 1;
	public static final int SETUP = 2;
	public static final int HELP = 3;
    public static final int CONNECT = 4;
    public static final int DISCONNECT = 4;

    public static final int COMPONENTS = 5;

    private EIRC eirc;

    private Resources res;
    private Component [] components;
	private boolean [] enabled;

    private boolean connected;

    public ControlPanel(EIRC eirc, boolean enabled[])
    {
    	this.eirc = eirc;
    	this.components = new Component[COMPONENTS];
		this.enabled = enabled;

    	FlowLayout layout = (FlowLayout) getLayout();
    	layout.setHgap(0);
    	layout.setVgap(0);
    	layout.setAlignment(FlowLayout.RIGHT);

    	ImageButton ib;

		if (enabled[ControlPanel.CHANNELS])
    		components[CHANNELS] = ib = new ImageButton(res.getLabel("panel.text.chanlist"), res.getImage("panel.icon.chanlist"));
		if (enabled[ControlPanel.WHO])
	    	components[WHO] = ib = new ImageButton(res.getLabel("panel.text.who"), res.getImage("panel.icon.who"));
		if (enabled[ControlPanel.SETUP])
	    	components[SETUP] = ib = new ImageButton(res.getLabel("panel.text.conf"), res.getImage("panel.icon.conf"));
		if (enabled[ControlPanel.HELP])
		   	components[HELP] = ib = new ImageButton(res.getLabel("panel.text.help"), res.getImage("panel.icon.help"));
    	
    	// This component's images are loaded later from setConnect.
		if (enabled[ControlPanel.CONNECT])
	    	components[CONNECT] = new ImageButton();

    	for (int i = 0; i < COMPONENTS; i++)
    	{
			if (components[i] != null)
			{
	    		add(components[i]);
    		    if (components[i] instanceof ImageButton)
    			{
    				ib = (ImageButton) components[i];
    				ib.addActionListener(this);
    		    }
			}
    	}

    	// Set default button states.
    	setConnected(false);
    }

    public void setConnected(boolean connected)
    {
		this.connected = connected;
		ImageButton ib = null;
		
		if (components[CONNECT] != null)
		{
			ib = (ImageButton)components[CONNECT];
			ib.setText(connected ? res.getLabel("panel.text.disconnect") : res.getLabel("panel.text.connect"));
			ib.setIcon(connected ? res.getImage("panel.icon.disconnect") : res.getImage("panel.icon.connect"));
		}

		if (components[CHANNELS] != null)
			components[CHANNELS].setEnabled(connected);

		if (components[WHO] != null)
			components[WHO].setEnabled(connected);

		// This must be here, or the "connect" button would disappear in MSIE if connection failed.
		validate();

		if (ib != null)
			ib.repaint();
    }

	public void repaint()
	{
		super.repaint();
		for (int i = 0; i < COMPONENTS; i++)
			if (components[i] != null)
			    components[i].repaint();
	}
	
	public void setFont(Font f)
    {
		for (int i = 0; i < COMPONENTS; i++)
			if (components[i] != null)
			    components[i].setFont(f);
    }
		
    public void setBackground(Color c)
    {
		super.setBackground(c);
		for (int i = 0; i < COMPONENTS; i++)
			if (components[i] != null)
			    components[i].setBackground(c);
    }

    public void setForeground(Color c)
    {
		super.setForeground(c);
		for (int i = 0; i < COMPONENTS; i++)
			if (components[i] != null)
			    components[i].setForeground(c);
    }

    public void actionPerformed(ActionEvent ev)
    {
    	Object source = ev.getSource();

    	if (source.equals(components[CONNECT]))
    	{
    	    if (connected)
			{
    			eirc.disconnect();
				eirc.openASL();
			}
    	    else
			{
    			eirc.connect();
			}
    	}
		
    	if (source.equals(components[CHANNELS]))
		{
    	    eirc.openChannelList();
		}

    	if (source.equals(components[SETUP]))
		{
    	    eirc.openConfigurator();
		}

      	if (source.equals(components[WHO]))
		{
      	    eirc.openWhoList();
		}

    	if (source.equals(components[HELP]))
		{
    	    eirc.openHelp();
		}
    }
}
